{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.  调用模型进行测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "from xgboost import XGBClassifier\n",
    "import xgboost as xgb\n",
    "\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.model_selection import StratifiedKFold\n",
    "\n",
    "from sklearn.metrics import log_loss\n",
    "\n",
    "from matplotlib import pyplot\n",
    "import seaborn as sns\n",
    "import pickle\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "读取数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(74659, 227)\n",
      "(49352, 228)\n"
     ]
    }
   ],
   "source": [
    "dpath = './data/'\n",
    "train = pd.read_csv(dpath+'RentListingInquries_FE_train.csv')\n",
    "# train = train.iloc[:1000,:]\n",
    "y_train = train['interest_level']\n",
    "X_train = train.drop('interest_level',axis=1)\n",
    "X_train = np.array(X_train)\n",
    "test = pd.read_csv(dpath+'RentListingInquries_FE_test.csv')\n",
    "# test = test.iloc[:100,:]\n",
    "# y_test = test?['interest_level']\n",
    "# X_test = test.drop('interest_level',axis=1)\n",
    "# X_test = np.array(X_test)\n",
    "test.head()\n",
    "test.shape\n",
    "X_test = np.array(test)\n",
    "# X_test = test\n",
    "# X_test = X_test[:1000,:]\n",
    "print(test.shape)\n",
    "print(train.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# load model from file\n",
    "# xgbmodel  = pickle.load(open(\"model.pickle.dat\", \"rb\"))\n",
    "# test_predprob = xgbmodel.predict_proba(X_test)\n",
    "# logloss = log_loss(y_test, test_predprob)\n",
    "xgb6 = XGBClassifier(\n",
    "        learning_rate =0.1,\n",
    "        n_estimators=181,  # 根据第4个文件，得到最优值是181\n",
    "        max_depth=7, # 根据上一轮结果，得到最优深度是7，weight是7\n",
    "        min_child_weight=7,\n",
    "        reg_alpha=1,\n",
    "        reg_lambda=0.75,# 根据上一轮结果，给出正则参数\n",
    "        gamma=0,\n",
    "        subsample=0.3,\n",
    "        colsample_bytree=0.95,\n",
    "        colsample_bylevel=0.8, # 上轮调参结果\n",
    "        objective= 'multi:softprob',\n",
    "        seed=3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 训练模型\n",
    "xgb6.fit(X_train, y_train)\n",
    "preds = xgb6.predict(X_test)\n",
    "y_pred = pd.DataFrame(preds)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.478008279807\n"
     ]
    }
   ],
   "source": [
    "#Predict training set:\n",
    "train_predprob = xgb6.predict_proba(X_train)\n",
    "logloss = log_loss(y_train, train_predprob)\n",
    "print(logloss)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "y_pred.to_csv('predict_results_with_header.csv',header=['Interest_level'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.axes._subplots.AxesSubplot at 0x10f446a58>"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAZUAAAD8CAYAAAC/1zkdAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMS4wLCBo\ndHRwOi8vbWF0cGxvdGx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THM02GyulhrNfVSOovyRsZ\nBMqnq+r3pz2eaTNUTg++rkaagiQB7gWeqaoPT3s8rweGymmgqg4DR15X8wzwgK+rWTyS/C7wCPDT\nSeaSbJr2mDS2q4FfA96e5Im2XD/tQU2TU4olSd14pSJJ6sZQkSR1Y6hIkroxVCRJ3RgqkqRuDBVJ\nUjeGiiSpG0NFktTN/wMSEIG0LOY6UQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<matplotlib.figure.Figure at 0x116bc26a0>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot = sns.countplot(preds)\n",
    "plot.savefig('predict_result_countplot.png')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(74659,)"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "preds.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
